package com.xtaller.lazy;

import com.xtaller.lazy.lib.generator.Code;
import com.xtaller.lazy.lib.generator.Conn;
import com.xtaller.lazy.lib.generator.Files;
import lombok.var;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-04-02
 * @Description: 代码生成
 */
public class TableGenerator {
    private String
            db = "tpv3", table = "", module = "",
            model = "",
            bigModel = "",
            modelPackageName = "",
            modelImportName = "",

            packageService = "",
            description = "";
    private StringBuffer codeModel,codeMapper,codeService;
    private Integer tableId = 0;

    public static void main(String[] args)  {
        try {
            new TableGenerator().run("base_department", "base");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run(String tableName, String moduleName) throws SQLException, IOException{
        this.table = tableName;
        this.module = moduleName;
        this.model = formatFirstTableName(table);
        this.modelPackageName = "com.xtaller.lazy.mapping." + module;
        this.modelImportName = "com.xtaller.lazy.mapping." + module + "." + formatFirstTableName(table);
        this.packageService = "com.xtaller.commounity.service." + module;

        Conn conn = new Conn(db, table);

        File directory = new File("");
        //得到的是C:/
        var dir = directory.getCanonicalPath();

        System.err.println(dir);

        var info = conn.tableInfo();
        if(info != null) {
            while (info.next()) {
                String item = info.getString(2);
                String[] data = item.split(" ");
                tableId = Integer.parseInt(data[1]);
                description = data[0];
            }
            var column = conn.tableColumn();
            codeModel = Code.createModel(module, model, table, tableId, description, column);
            codeMapper = Code.createMapper(module, model, description);
            codeService = Code.createService(module, model, description);


        }


        // 文件保存路径
        String modelOutputDir = dir + "/lazy-core/src/main/java/com/xtaller/lazy/model/" + module ;
        String serviceOutputDir = dir + "/lazy-core/src/main/java/com/xtaller/lazy/service/" + module;
        String mapperOutputDir = dir + "/lazy-core/src/main/java/com/xtaller/lazy/mapper/" + module ;
        var modelName = model + ".java";
        var mapperName = model + "Mapper.java";
        var serviceName = model + "Service.java";

        Files.write(modelOutputDir, modelName, codeModel, true);
        Files.write(mapperOutputDir, mapperName, codeMapper, false);
        Files.write(serviceOutputDir, serviceName, codeService, false);

    }    // 生成前端表格字段
    private static void createJsField(Conn conn) {
        var rs = conn.tableColumn();
        try {
            while (rs.next()) {
                System.out.println("{field: '"+rs.getString(1)+"', title: '"+rs.getString(3)+"', align: 'center'},");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String formatTableName(String field){
        String[] item = field.split("_");
        int len = item.length;
        String name = "";
        if(len > 1){
            for(int i=0;i<len;i++){
                name += (item[i].substring(0, 1).toUpperCase()+ item[i].substring(1, item[i].length()));
            }
        }else{
            name = field.substring(0, 1).toUpperCase()+ field.substring(1,field.length());
        }
        return name;
    }
    private static String formatFirstTableName(String field){
        String[] item = field.split("_");
        String name = "";
        for(int i=1;i<item.length;i++){
            name += item[i].substring(0, 1).toUpperCase()+ item[i].substring(1,item[i].length());
        }
        return name;
    }

}
